package com.windupurnomo.data;

import com.windupurnomo.entity.Player;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataModel {
	public SQLiteDatabase dataBase = null;
	public Cursor cursor = null;
	
	public DataModel(Context param){
		dataBase=new ActivityManagerData(param).getWritableDatabase();
	}
	
	public boolean saveNewPlayer(SQLiteDatabase db, Player player) {
		ContentValues cv = new ContentValues();
		cv.put("name",player.getName());
		cv.put("sex", player.getSex());
		db.insert("player", "xx", cv);
		return true;
		
		/*boolean success=false;
			try{
				String sql="INSERT INTO player(name, sex, point) VALUES" +
						"('"+player.getName()+"','"+player.getSex()+"',"+player.getPoint()+")";
				dataBase.execSQL(sql);
				success=true;
			}catch (Exception e) {}
		return success;*/
	}

	public Cursor getPlayers() {
		try {
			String sql="SELECT * FROM player";
			cursor=dataBase.rawQuery(sql,null);
		} catch (Exception e) {}
		return cursor;
	}

	public Player getPlayerById(int id) {
		Player selectedPlayer = new Player();
		try{
			String sql="SELECT * FROM notes WHERE id='"+id+"' ";
			cursor = dataBase.rawQuery(sql, null);
			selectedPlayer.setId(id);
			selectedPlayer.setName(cursor.getString(1));
			selectedPlayer.setSex(cursor.getString(2));
			
		}catch (Exception e) {}
		return selectedPlayer;
	}

	public Player cursorToPlayer(Cursor param) {
		Player selectedPlayer = new Player();
		selectedPlayer.setName(param.getString(1));
		selectedPlayer.setSex(param.getString(2));
		return selectedPlayer;
	}
}
